// pages/home/obtained/obtained.js
var util = require('../../../utils/util.js')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    hiddenLoading: true,
    shelfCode: "",
    products: [],
    itemNumbers: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log("onshow的方法");
    var that = this;
    that.setData({
      hiddenLoading: false
    });
    var app = getApp();
    var shelfCode = app.globalData.shelfCode;
    util.getRequest('shelf/shelfInfo/' + shelfCode + '/true', '', 'GET', function(result) {
        if (typeof(result) != 'undefined' && result != null) {
          if (!result.success) {
            that.setData({
              // hiddenDataView: true,
              hiddenLoading: true
            })
            if (typeof(result.message) != 'undefined' && result.message != null && result.message != '') {
              that.showPrompt(result.message);
            } else {
              that.showPrompt('查询失败');
            }
          } else {
            console.log("====");
            console.log(result.data);
            that.setData({
              hiddenLoading: true,
              products: result.data.products,
            })
            var length = result.data.products.length;
            var productsTemp = result.data.products;
            if (length > 0) {
              for (var i in result.data.products) {
                var isSelectedParam = "products[" + i + "].isSelected"
                var maxQtyParam = "products[" + i + "].maxQty"
                that.setData({
                  [isSelectedParam]: false,
                  [maxQtyParam]: productsTemp[i].qty
                })
              }
              // console.log("添加了最大数量的字段:"+products);
            } else {
              that.showPrompt('当前货架无商品');
            }
          }
        } else {
          that.showPrompt('查询失败');
        }
      },
      function(failedResult) {
        console.log('failedResult');
        if (failedResult.data != undefined && failedResult.data != null) {
          console.log(failedResult.data);
          that.showPrompt(failedResult.data.message);
        }
      });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },


  /**
   * 绑定加数量事件
   */
  btn_add(e) {
    // 获取点击的索引
    const index = e.currentTarget.dataset.index;
    // 获取商品数据
    let products = this.data.products;
    console.log(products.length);
    // 获取商品数量
    let qty = products[index].qty;
    let qtyMax = products[index].maxQty;
    if (qty >= qtyMax) {
      this.showPromptTwo("该商品已达到最大数量");
    } else {
      // 点击递增
      qty = qty + 1;
      products[index].qty = qty;
      // 重新渲染 ---显示新的数量
      this.setData({
        products: products
      });
    }
  },

  /**
   * 绑定减数量事件
   */
  btn_minus(e) {
    //   // 获取点击的索引
    const index = e.currentTarget.dataset.index;
    // const obj = e.currentTarget.dataset.obj;
    // console.log(obj);
    // 获取商品数据
    let products = this.data.products;
    // 获取商品数量
    let qty = products[index].qty;
    // 判断num小于等于1  return; 点击无效
    if (qty <= 1) {
      return false;
    }
    // else  num大于1  点击减按钮  数量--
    qty = qty - 1;
    products[index].qty = qty;
    // 渲染页面
    this.setData({
      products: products
    });
  },

  /**
   * 当前商品选中事件
   */
  selectList(e) {
    console.log("执行接口");
    var that = this;
    // var obj = e.detail.value;
    // console.log(obj);
    // 获取选中的radio索引
    var index = e.currentTarget.dataset.index;
    // 获取到商品列表数据
    var products = that.data.products;
    console.log(products);
    // 默认全选
    that.data.selectAllStatus = true;
    // 循环数组数据，判断----选中/未选中[selected]
    products[index].isSelected = !products[index].isSelected;
    // 如果数组数据全部为selected[true],全选
    for (var i = products.length - 1; i >= 0; i--) {
      if (!products[i].isSelected) {
        that.data.selectAllStatus = false;
        break;
      }
    }
    // 重新渲染数据
    that.setData({
      products: products,
      selectAllStatus: that.data.selectAllStatus
    })
  },

  /**
   * 列表点击事件
   */
  itemclick: function(e) {
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index);
    var app = getApp();
    app.globalData.inquireScanCode = that.data.products[index].itemNumber;
    wx.navigateTo({
      url: '../inquire/productDetails/productDetails',
    })
  },

  /**
   * 下架
   */
  btn_down(e) {
    var that = this;
    var app = getApp();
    var shelfCode = app.globalData.shelfCode;
    var products = that.data.products;
    var itemNumbers = that.data.itemNumbers;
    console.log("修改的商品数量");
    console.log(itemNumbers);
    console.log(products);
    for (var i = 0; i <= products.length - 1; i++) {
      var isSelected = products[i].isSelected;
      if (isSelected) {
        console.log(i + "选中了");
        var obj = {};
        obj.itemNumber = products[i].itemNumber;
        obj.qty = products[i].qty;
        itemNumbers.push(obj);
      }
    }
    console.log(itemNumbers);
    console.log(shelfCode);
    if (itemNumbers.length > 0) {
      that.setData({
        hiddenLoading: false
      });
      util.getRequest('shelf/itemShelf/', {
          "itemNumbers": itemNumbers,
          "shelfCode": shelfCode,
          "type": 1
        }, 'POST', function(result) {
          console.log(result);
          if (typeof(result) != 'undefined' && result != null) {
            if (!result.success) {
              that.setData({
                // hiddenDataView: true,
                hiddenLoading: true
              })
              if (typeof(result.message) != 'undefined' && result.message != null && result.message != '') {
                that.showPromptTwo(result.message);
                that.setData({
                  itemNumbers: []
                })
              } else {
                that.showPrompt('操作失败');
              }
              that.setData({
                itemNumbers: []
              })
            } else {
              console.log(result.data);
              that.setData({
                itemNumbers: []
              })
              that.setData({
                hiddenLoading: true,
              })
              that.showPromptTwo('操作成功');
              that.onShow();
            }
          } else {
            that.showPrompt('操作失败');
          }
        },
        function(failedResult) {
          console.log('failedResult');
          if (failedResult.data != undefined && failedResult.data != null) {
            console.log(failedResult.data);
            that.showPrompt(failedResult.data.message);
          }
        });
    } else {
      that.showPromptTwo('请选择要下架的商品');
    }

  },

  /**
   * 一键下架
   */
  btn_down_all(e) {
    var that = this;
    that.setData({
      hiddenLoading: false
    });
    var app = getApp();
    var shelfCode = app.globalData.shelfCode;
    var products = that.data.products;
    var itemNumbers = that.data.itemNumbers;
    console.log("修改的商品数量");
    console.log(itemNumbers);
    console.log(products);
    for (var i = 0; i <= products.length - 1; i++) {
      var isSelected = products[i].isSelected;
      var obj = {};
      obj.itemNumber = products[i].itemNumber;
      obj.qty = products[i].qty;
      itemNumbers.push(obj);
    }
    // console.log(itemNumbers);
    util.getRequest('shelf/itemShelf/', {
        "itemNumbers": itemNumbers,
        "shelfCode": shelfCode,
        "type": 1
      }, 'post', function(result) {
        console.log(result);
        if (typeof(result) != 'undefined' && result != null) {
          if (!result.success) {
            that.setData({
              // hiddenDataView: true,
              hiddenLoading: true
            })
            if (typeof(result.message) != 'undefined' && result.message != null && result.message != '') {
              that.showPromptTwo(result.message);
              that.setData({
                itemNumbers: []
              })
            } else {
              that.showPrompt('操作失败');
            }
          } else {
            console.log(result.data);
            that.setData({
              itemNumbers: []
            })
            that.setData({
              hiddenLoading: true,
            })
            that.showPrompt('操作成功');
          }
        } else {
          that.showPrompt('操作失败');
        }
      },
      function(failedResult) {
        console.log('failedResult');
        if (failedResult.data != undefined && failedResult.data != null) {
          console.log(failedResult.data);
          that.showPrompt(failedResult.data.message);
        }
      });
  },
  /**
   * 提示并退出
   */
  showPrompt(content) {
    wx.showToast({
      title: content,
      icon: 'none',
      duration: 2000
    })
    setTimeout(function() {
      wx.navigateBack()
    }, 2500)
  },
  /**
   * 提示
   */
  showPromptTwo(content) {
    wx.showToast({
      title: content,
      icon: 'none',
      duration: 2000
    })
  },

  bindKeyInput: function(e) {
    const index = e.currentTarget.dataset.index;
    let products = this.data.products;
    var qty = e.detail.value;
    console.log("键盘修改后的数量："+qty);
    if (qty > products[index].maxQty) {
      this.showPromptTwo("该商品已达到最大数量");
    }
      products[index].qty = qty;
      this.setData({
        products: products
      });
    console.log("键盘修改后商品列表对象的数量：" + this.data.products[index].qty);
  }
})